/**
 * Created by malingling on 15-12-15.
 */
app.controller ('SelectMenuCtrl', function ($scope, $location) {

    disabled($scope)
    combo_disabled($scope)

    $scope.go_to_select_people = function () {
        $location.path('select_people')
    }


    $scope.go_to_select_restaurant = function () {
        $location.path('select_restaurant')

    }


    $scope.go_to_select_combo = function () {
        $location.path('select_combo')

    };


    $scope.back_to_order = function () {
        $location.path('')
    }

    $scope.person = localStorage.getItem('person')
    $scope.restaurant = localStorage.getItem('restaurant')
    $scope.name = localStorage.getItem ('combo_name')

    combo_disabled($scope)

    disabled($scope)

    $scope.commit = function () {
        var inform = {};
        inform.person = localStorage.getItem('person')
        inform.restaurant = localStorage.getItem('restaurant')
        inform.name = localStorage.getItem ('combo_name')
        inform.price = localStorage.getItem ('combo_price')

        if (JSON.parse (localStorage.getItem('all_informs')) == null) {

            var informs = [];

        }
        else {
            var informs = JSON.parse (localStorage.getItem('all_informs'))
        }
        informs.push(inform)

        var all_informs = JSON.stringify (informs)

        localStorage.setItem('all_informs', all_informs)
        informs.forEach(function (inform) {
            if (inform.person == localStorage.getItem('person')) {
                inform.restaurant = $scope.restaurant
                inform.name = localStorage.getItem ('combo_name')
                inform.price = localStorage.getItem ('combo_price')
            }
        })


        clean($scope)
        disabled($scope)
        combo_disabled($scope)
    }

})

function clean($scope) {
    $scope.person = '';
    $scope.restaurant = '';
    $scope.name = '';
    localStorage.removeItem('person')
    localStorage.removeItem('restaurant')
    localStorage.removeItem('combo_name')
    localStorage.removeItem('combo_price')
}

function disabled($scope) {

    document.getElementById("disabled").disabled =
        !$scope.person || !$scope.restaurant || !$scope.name;
    //console.log(disabled.disabled);
}



function combo_disabled($scope) {
    document.getElementById("combo").disabled =
        (! $scope.restaurant)
}